package com.dream.wukong.common.core.importexport.csv.parser;

import com.univocity.parsers.common.ParsingContext;
import com.univocity.parsers.common.processor.AbstractRowProcessor;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import com.univocity.parsers.csv.CsvWriter;
import com.univocity.parsers.csv.CsvWriterSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @Author: wuyichao
 * @Description:
 * @Date: Created in 16:35 2018/9/17.
 * @Modified:
 */
public class UnivocityParser extends AbstractParser {

    private static final Logger logger = LoggerFactory.getLogger(UnivocityParser.class);


    public UnivocityParser() {
        super("uniVocity CSV parser");
    }

    @Override
    public void processRows(File input) {
        CsvParserSettings settings = new CsvParserSettings();

        // turning off features enabled by default
        settings.setIgnoreLeadingWhitespaces(false);
        settings.setIgnoreTrailingWhitespaces(false);
        settings.setSkipEmptyLines(false);
        settings.setColumnReorderingEnabled(false);
        settings.setRowProcessor(new AbstractRowProcessor() {
            @Override
            public void rowProcessed(String[] row, ParsingContext context) {
                process(row);
            }
        });

        CsvParser parser = new CsvParser(settings);
        parser.parse(toReader(input,encoding));
    }

    @Override
    public List<String[]> parseRows(File input) {
        CsvParserSettings settings = new CsvParserSettings();
        CsvParser parser = new CsvParser(settings);
        return parser.parseAll(toReader(input,encoding));
    }

    @Override
    public File writeRows(List<Object[]> rows) {
        File tmpFile ;
        try {
            tmpFile = File.createTempFile(UUID.randomUUID().toString(), ".csv");
            writeRows(rows,tmpFile.getAbsolutePath());
        } catch (IOException e) {
            logger.error(e.getMessage());
            return null;
        }
        return tmpFile;
    }

    @Override
    public File writeRows(List<Object[]> rows, String filepath) {
        logger.debug("写入数据到"+filepath);
        File csvfile = new File(filepath);
        Writer outputWriter = toWriter(csvfile,encoding);
        CsvWriter writer = new CsvWriter(outputWriter, new CsvWriterSettings());
        writer.writeRowsAndClose(rows);
        return csvfile;
    }


    public static void main(String[] args) throws IOException {
//		List<Object[]> rows = Arrays
//				.asList(new Object[][] {
//						{ "1997", "Ford", "E350", "ac, abs, moon", "3000.00" },
//						{ "1999", "Chevy", "Venture \"Extended Edition\"", "",
//								"4900.00" },
//						{ "1996", "Jeep", "Grand Cherokee",
//								"MUST SELL!\nair, moon roof, loaded", "4799.00" },
//						{},
//						{ "1999", "Chevy",
//								"Venture \"Extended Edition, Very Large\"",
//								null, "5000.00" },
//						{ null, "", "Venture中文 \"Extended Edition\"", null,
//								"4900.00" }, });
//		UnivocityParser UnivocityParser = new UnivocityParser();
//		UnivocityParser.writeRows(rows);
// 		UnivocityParser.writeRows(rows,"d://efg1.csv");
//		byte[] BOM = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };
        File file =  new File("f://aaaa.csv");
        UnivocityParser univocityParser = new UnivocityParser();
        univocityParser.setEncoding("utf-8");
        List<String[]> rowStrings =univocityParser .parseRows(file);
        for (String[] strings : rowStrings) {
            System.out.println(Arrays.asList(strings));
        }

    }

}
